#include "Control.h"
#include "View.h"
#include "StuMode.h"

void Control::exec()
{
	View v;
	Mode* instance = Mode::getInstance();
	instance->loadStudent("stu.txt");
	//界面显示，菜单的选择
	while(1){
		v.loginMenu();
		int choice = 0;
		cin >> choice;
		switch(choice){
			case 1:		//管理员登录 admin admin
				if(adminLogin()){
					cout << "登录成功" << endl;
					adminControl();
				}else{
					cout << "用户名和密码不正确，请核对你的用户名和密码" << endl;
				}
				break;
			case 2:		//学生登录
				break;	
			case 0:		//退出
				instance->saveStudent("stu.txt");
				return ;
			default:
					break;
		
		}
	
	}
}


void Control::adminControl()
{
	View v;
	Mode* instance = Mode::getInstance();
	while(1){
		v.adminMenu();
		#if 0
				cout << "1 注册学生" << endl;
				cout << "2 注销学生" << endl;
				cout << "3 显示学生" << endl;
				cout << "4 按成绩排序" << endl;
				cout << "5 查找学生" << endl;
				cout << "请输入你的选择: "
		#endif
			
			int choice = 0;
			cin >> choice;
			switch(choice){
				case 1:			//注册
					registerUser();
					break;	
				case 2:			//注销
					deleteStudent();
					break;
				case 3:			//显示
					instance->display();
					break;
				case 4:			//排序
					break;
				case 5:			//查找
					break;
				case 0:			//退出
					return ;
				default:
					break;
			}
		
	
	}

}

	//学生注销
void Control::deleteStudent()
{
	Mode* instance = Mode::getInstance();
	instance->display();
	int id  = 0;
	cout << "请输入要注销的学生ID: ";
	cin >> id;
	instance->removeStudent(id);
}

void Control::registerUser()
{
	static int stuId = 20191000;
	string name, pwd;
	cout << "请输入学生姓名：";
	cin >> name;
	cout << "请输入学生密码: ";
	cin >> pwd;
	
	Student stu(stuId++, name, pwd);
	
	//添加
	Mode* instance = Mode::getInstance();
	instance->insertStudent(stu);
}

bool Control::adminLogin()
{
	string name, pwd;
	cout << "请输入管理员用户名: ";
	cin >> name;
	cout << "请输入管理员密码";
	cin >> pwd;
	
	if(name == "admin" && pwd == "admin"){
		return true;
	}else{
		return false;
	}
}


